//把配置项单独处理

import $C from '@/common/config.js'; // 引入baseUrl

export default {
	//定义默认请求参数
	common: {
		method: 'GET',
		header: $C.leanHeader,
		data: {}
	},
	request(options = {}) {
		options.url = $C.leanUrl + options.url
		options.method = options.method || this.common.method
		options.header = options.header || this.common.header

		return new Promise((res, rej) => {
			uni.request({
				...options,

				success: (result) => {
					// 请求服务端失败
					if (result.statusCode !== 200 && !options.notoast && result.statusCode !==201) {
						uni.showToast({
							title: result.data.msg || '请求失败',
							icon: 'none'
						});
						return rej(result)
					}
					// 成功
					res(result)
				},
				fail: (error) => {
					uni.showToast({
						title: error.errMsg || '请求失败',
						icon: 'none'
					});
					return rej(error)
				}
			});
		})
	},
	// 单独封装 get 方式请求
	get(url, data = {}, options = {}) {
		options.url = url
		options.data = data
		options.method = 'GET'
		return this.request(options)
	},
	// 单独封装 post 方式请求
	post(url, data = {}, options = {}) {
		options.url = url
		options.data = data
		options.method = 'POST'
		return this.request(options)
	},
  // 单独封装 put 方式请求
  put(url, data = {}, options = {}) {
  	options.url = url
  	options.data = data
  	options.method = 'PUT'
  	return this.request(options)
  },
  // 单独封装 put 方式请求
  delete(url, data = {}, options = {}) {
  	options.url = url
  	options.data = data
  	options.method = 'Delete'
  	return this.request(options)
  },
}
